<template>
  <a-modal
    :title="title"
    :width="800"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭">
    
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="展会主题">
          <a-select :value="valSell" @change="productNum" v-decorator="['exhibitionMeetingId', {rules: [{ required: true, message: '请选择展会主题!' }]}]">
            <a-select-option value="">请选择</a-select-option>
            <a-select-option v-for="(item, index) in dictOptions" :key="item.index" :value="item.id">
              <span style="display: inline-block;width: 100%">
                {{ item.theme }}
              </span>
            </a-select-option>
          </a-select>
        </a-form-item>

        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="在售剧本">
          <a-checkbox-group @change="onChangeOld" v-decorator="['worksIds', {}]" >
            <a-checkbox v-for="(item, key) in specification" :value="key">{{ item.productName }}</a-checkbox>
          </a-checkbox-group>
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="预售剧本">
          <a-checkbox-group @change="onChangeNew" v-decorator="['newWorksIds', {}]" >
            <a-checkbox v-for="(item, key) in specificationNew" :value="key">{{ item.productName }}</a-checkbox>
          </a-checkbox-group>
        </a-form-item>
        <a-form-item
          class="checkbxGroup"
          style="overflow: hidden;"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="选择展位">
          <a-checkbox-group style="width:575px;white-space:nowrap;" v-for="(val,index) in enList.ens"  v-decorator="['boothNum', {}]">
            <a-checkbox v-for="(item, key) in numList" :disabled="boothNums.indexOf(val+item)!=-1" :value="val+item">{{ val+item }}</a-checkbox>
          </a-checkbox-group>
        </a-form-item>
		
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
  import { httpAction } from '@/api/manage'
  import pick from 'lodash.pick'
  import moment from "moment"
  import { getAction,postAction } from '@/api/manage'

  export default {
    name: "Exhibition_issuer_registerModal",
    data () {
      return {
        title:"操作",
        enList:{},
        numList:[],
        boothNums:"",
        visible: false,
        model: {},
        dictOptions:[],
        specification:[],
        specificationNew:[],
        valSell:"",
        listNameOld:"",
        listNameNew:"",
        listIdOld:"",
        listIdNew:"",
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },

        confirmLoading: false,
        form: this.$form.createForm(this),
        validatorRules:{
        },
        url: {
          add: "/exhibition/exhibition_issuer_register/add",
          edit: "/exhibition/exhibition_issuer_register/edit",
        },
      }
    },
    mounted () {
      var params={
          order: "asc",
          field: "",
          pageNo: 1,
          pageSize: 1000
        };
        getAction("/exhibition/exhibition_meeting_info/list",params).then((res) => {
          if (res.success) {
            this.dictOptions = res.result.records;
          }
          if(res.code===510){
            this.$message.warning(res.message)
          }
        })
        var param={
          productStandardsId: "1216548480059949057",
          order: "asc",
          field: "",
          pageNo: 1,
          pageSize: 1000
        };
        getAction("/exhibition/exhibition_product_standards_new/list",param).then((res) => {
          if (res.success) {
            var list = res.result.records;
            // this.specificationNew = res.result.records;
            for(let i=0;i<list.length;i++){
              if(list[i].newOldStatus == 0){
                this.specification.push(list[i]);
              }
              if(list[i].newOldStatus == 1){
                this.specificationNew.push(list[i]);
              }
            }
          }
          if(res.code===510){
            this.$message.warning(res.message)
          }
        })
    },
    methods: {
      productNum(value){
        postAction("/exhibition/exhibition_issuer_register/getBoothNumByMeetingId",{exhibitionMeetingId:value}).then((res) => {
          if (res.success) {
            this.boothNums = res.result.boothNums;
            var column = res.result.exhibition_meeting_info.columnNumber;
            var line = res.result.exhibition_meeting_info.lineNumber;
            this.numList = [];
            this.enList.ens = [];
            this.enList.nums = [];
            for (var i=0 ;i<column ; i++){
                this.enList.ens.push(String.fromCharCode(i+65)); 
                this.enList.nums.push(i+1);
            }
            for (var j=0 ;j<line ; j++){
                this.numList.push(j+1); 
            }
          }else{
            this.$message.warning(res.message)
          }
        })
      },
      onChangeNew(value){
        this.listNameNew = ""
        this.listIdNew = ""
        for(let i=0;i<value.length;i++){
          if(i == 0){
            this.listNameNew += this.specificationNew[value[i]].productName;
            this.listIdNew += this.specificationNew[value[i]].id;
          }else{
            this.listNameNew +="," + this.specificationNew[value[i]].productName;
            this.listIdNew += "," + this.specificationNew[value[i]].id;
          }
        }
      },
      onChangeOld(value){
        this.listNameOld = ""
        this.listIdOld = ""
        for(let i=0;i<value.length;i++){
          if(i == 0){
            this.listNameOld += this.specification[value[i]].productName;
            this.listIdOld += this.specification[value[i]].id;
          }else{            
            this.listNameOld +="," +  this.specification[value[i]].productName;
            this.listIdOld +="," +  this.specification[value[i]].id;
          }
        }
      },
      add () {
        this.edit({});
      },
      edit (record) {
        this.form.resetFields();
        this.model = Object.assign({}, record);
        this.visible = true;
        this.$nextTick(() => {
          this.form.setFieldsValue(pick(this.model,'exhibitionMeetingId','boothNum'))
		  //时间格式化
        });

      },
      close () {
        this.$emit('close');
        this.visible = false;
      },
      choockeList(val){
        var list = "";
        for(let i=0;i<val.length;i++){
          if(i==0){
            list+=val[i];
          }else{
            list+=","+val[i];
          }
        }
        return list
      },
      handleOk () {
        const that = this;
        // 触发表单验证
        this.form.validateFields((err, values) => {
          if (!err) {
            console.log(values)
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!that.model.id){
              httpurl+=that.url.add;
              method = 'post';
            }else{
              httpurl+=that.url.edit;
               method = 'put';
            }
            values.worksIds = this.listIdOld;
            values.newWorksIds = this.listIdNew;
            values.worksIdsName = this.listNameOld;
            values.newWorksIdsName = this.listNameNew;
            if(!this.listIdOld && !this.listIdNew){
              that.$message.warning("请先选择剧本！");
              return
            }
            if(!values.boothNum){
              that.$message.warning("请先展位！");
              return
            }
            var strVal = "";
            for(var i=0;i<values.boothNum.length;i++){
              strVal+=values.boothNum[i]+";";
            }
            values.boothNum = strVal;
            let formData = Object.assign(that.model, values);
            //时间格式化
            
            httpAction(httpurl,formData,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
              that.close();
            })
          }
        })
      },
      handleCancel () {
        this.close()
      },


    }
  }
</script>

<style lang="less" scoped>
  .checkbxGroup /deep/ .ant-checkbox-group{
    overflow-x: auto;
  }
</style>